//
//  WSPlayerRequest.m
//  CharonTVProject
//
//  Created by karong on 2023/8/3.
//

#import "WSPlayerRequest.h"
#import "WSVideoDetail.h"

@implementation WSPlayerRequest

+ (void)loadcharonTV_PlayerListWithPage:(NSInteger)page success:(charonTV_ArrayBlock)success failed:(Failure)failed {
    NSString * charonTV_url = API_CHARONTV_SHOTTV_RDOM;
    NSDictionary * charonTV_parameters = @{@"page":@(page),
                                           @"limit":@(50)
    };
    [self postWithURLString:charonTV_url parameters:charonTV_parameters success:^(NSDictionary * _Nonnull responseDict) {
        NSMutableArray * charonTV_array = [WSVideo mj_objectArrayWithKeyValuesArray:responseDict[@"items"]];
        success(charonTV_array);
    } failure:failed];
}

+ (void)loadcharonTV_FavoriteWithChapter_id:(NSInteger)chapter_id success:(charonTV_IntegerBlock)success failed:(Failure)failed {
    NSString * charonTV_url = API_CHARONTV_SHOTTV_INFO;
    NSDictionary * charonTV_parameters = @{@"chapter_id":@(chapter_id)};
    [self postWithURLString:charonTV_url parameters:charonTV_parameters success:^(NSDictionary * _Nonnull responseDict) {
        success([responseDict[@"is_like"] integerValue]);
    } failure:failed];
}

+ (void)favoritecharonTV_WithChapter_id:(NSInteger)chapter_id tv_id:(NSInteger)tv_id success:(charonTV_EmptyBlock)success failed:(Failure)failed {
    NSString * charonTV_url = API_CHARONTV_SHOTTV_FAVORITE;
    NSDictionary * charonTV_parameters = @{@"chapter_id":@(chapter_id),
                                           @"tv_id":@(tv_id)
    };
    [self postWithURLString:charonTV_url parameters:charonTV_parameters success:^(NSDictionary * _Nonnull responseDict) {
        success();
    } failure:failed];
}

+ (void)loadcharonTV_shotTVDetailWithID:(NSInteger)tvId success:(charonTV_AnyObjectBlock)success failed:(Failure)failed {
    NSString * charonTV_url = API_CHARONTV_SHOTTV_DETAIL;
    NSDictionary * charonTV_parameters = @{@"id":@(tvId)};
    [self postWithURLString:charonTV_url parameters:charonTV_parameters success:^(NSDictionary * _Nonnull responseDict) {
        WSVideoDetail * object = [WSVideoDetail mj_objectWithKeyValues:responseDict];
        for (WSVideo * video in object.chapter_list) {
            video.cover = object.cover;
            video.is_vip = object.pay_type == 1;
            video.tv_id = object.id;
        }
        success(object);
    } failure:failed];
}
#pragma mark - PagableRequestPotocol
- (void)refreshDataSuccess:(charonTV_ArrayBlock)success failed:(Failure)failed {
    if (self.loading) {
        return;
    }
    self.loading = YES;
    self.page = 1;
    self.haveMore = YES;
    [WSPlayerRequest loadcharonTV_PlayerListWithPage:self.page success:^(NSArray *array) {
        [self loadSuccess:success withArray:array];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        failed(response, error, responseObject);
        self.loading = NO;
    }];
}


- (void)loadMoreDataSuccess:(charonTV_ArrayBlock)success failed:(Failure)failed {
    if (self.loading && !self.haveMore) {
        return;
    }
    [WSPlayerRequest loadcharonTV_PlayerListWithPage:self.page success:^(NSArray *array) {
        [self loadSuccess:success withArray:array];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        failed(response, error, responseObject);
        self.loading = NO;
    }];
}

- (void)loadSuccess:(charonTV_ArrayBlock)success withArray:(NSArray *)array {
    self.haveMore = array.count >= 50;
    self.page++;
    self.loading = NO;
    success(array);
}

@end
